﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Bill : System.Web.UI.Page
{
    BillBUS billBUS = new BillBUS();
    BillDetailsBUS billDetailsBUS = new BillDetailsBUS();
    DataTable tbBill = new DataTable();
    DataView viewBill;
    DataTable tbBillDetails = new DataTable();
    BillGiftBUS biGiftBUS = new BillGiftBUS();
    DataTable tbBiGift = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;

        }
        LoadDataList();
    }

    protected void LoadDataList()
    {
        tbBill = billBUS.returnAllBill();
        viewBill = new DataView(tbBill);
        grvBill.DataSource = tbBill;
        grvBill.DataBind();
    }
    protected void grvBill_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.EndsWith("Upd"))
        {
            viewBill.RowFilter = "Bi_ID=" + Int32.Parse(e.CommandArgument.ToString());
            lblTTAmount.Text = viewBill[0]["Bi_TotalAmount"].ToString();
            lblAddr.Text = viewBill[0]["Bi_Address"].ToString();
            lblDDate.Text = viewBill[0]["Bi_DateCompleted"].ToString();
            lblODate.Text = viewBill[0]["Bi_Date"].ToString();
            lblMemID.Text = viewBill[0]["M_ID"].ToString();
            lblMessages.Text = viewBill[0]["Bi_Messages"].ToString();
            if (viewBill[0]["Bi_Status"].ToString().EndsWith("No delivered"))
            {
                drlStatus.SelectedIndex = 0;
            }
            else
            {
                drlStatus.SelectedIndex = 1;
            }
            MultiView1.ActiveViewIndex = 1;
            lblIDBill.Text = e.CommandArgument.ToString();
            tbBillDetails = billDetailsBUS.returnBillDetailsByID(Int32.Parse(e.CommandArgument.ToString()));
            tbBiGift = biGiftBUS.returnBillGiftByBiID(Int32.Parse(e.CommandArgument.ToString()));
            if (tbBiGift.Rows.Count > 0)
            {
                lblGift.Visible = true;
                grvGift.DataSource = tbBiGift;
                grvGift.DataBind();
            }

            if (tbBillDetails.Rows.Count > 0)
            {
                lblBouquet.Visible = true;
                grvBou.DataSource = tbBillDetails;
                grvBou.DataBind();

            }

        }

    }
    protected void grvBill_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvBill.PageIndex = e.NewPageIndex;
        LoadDataList();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        billBUS.updateBillStatus(Int32.Parse(lblIDBill.Text), drlStatus.SelectedValue.ToString());
    }
    protected void lbnSearch_Click(object sender, EventArgs e)
    {
        if (txtSearch.Value.Length > 0)
        {
            viewBill.RowFilter = "Bi_ID=" + txtSearch.Value.ToString();
            grvBill.DataSource = viewBill;
            grvBill.DataBind();

        }
    }
}